/*
 * DoNumber.h
 *
 *  Created on: Oct 18, 2012
 *      Author: wtao
 */

#ifndef DONUMBER_H_
#define DONUMBER_H_

#include "_DoObject.h"

class DoNumber: public _DoObject
{
public:
	enum eType {
		eInteger,
		eReal
	};
protected:
	DoNumber(const DoNumber&);
	DoNumber& operator=(const DoNumber&);

	int mInt;
	float mReal;
	eType mType;
		
	virtual CRefPtr< _DoObject > clone() {
		DoNumber *p0 = new DoNumber();
		_DoObject::clone(p0, this);
		
		p0->mType = getType();
		p0->mInt = getInt();
		p0->mReal = getReal();
		
		return CRefPtr< _DoObject >(p0);
	}
public:
	DoNumber();

	DoNumber(int value) : _DoObject() {
		mType = eInteger;
		mInt = value;
		mReal = (float)mInt;
		mObjType = eNumber;
	}

	DoNumber(float value) : _DoObject() {
		mType = eReal;
		mReal = value;
		mInt = (int) mReal;
		mObjType = eNumber;
	}

	virtual ~DoNumber();

	int getInt() {
		return mInt;
	}

	float getReal() {
		return mReal;
	}

	void parse(bool isReal, bool isRadix, std::string str);

	eType getType() {
		return mType;
	}
	
	virtual std::string _debug_dump() const{
		std::ostringstream oss;
		if (mType == eReal)
			oss << "f-" << mReal;
		else
			oss << "i-" <<mInt;

		return oss.str();
	}
	
	int compare(DoNumber *obj) {
		if (mInt == obj->getInt() && mReal == obj->getReal())
			return 0;
		if (mInt < obj->getInt() || mReal < obj->getReal())
			return -1;
		return 1;
	}
	
	virtual bool equals(CRefPtr< _DoObject > obj) {
		DoNumber *num = toNumber(obj);
		return compare(num) == 0;
	}
};

#endif /* DONUMBER_H_ */
